package com.zlq.cn;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory;
import com.zlq.cn.retrofit.RequestConfig;
import com.zlq.cn.retrofit.WanandroidResult;
import javazoom.jl.player.Player;
import one.util.streamex.StreamEx;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class HPV4 {
    private static final String REUS_TRUE_NAME = "蓝龙巧";

    public interface NewHealthApiService {
        // POST https://szhc.newhealth.com.cn/vacc/service?function=vaccineorder&method=reservationstockDate&visitcode=Pi0ynNDpmTJMf87i&token=0edc0814150e229f53eb628f6c66cd86
        @POST("/vacc/service?function=vaccineorder&method=vaccorglist&visitcode=Pi0ynNDpmTJMf87i&token=cc8c78bdac1c7a2dd18aaf12cba57016")
        @RequestConfig(resultClz = WanandroidResult.class, retryCount = 3)
        Call<JSONObject> vaccorglist(@Body JSONObject body);

        /**
         * 接种人列表
         */
        @POST("/vacc/service?function=vaccineorder&method=adultlist&visitcode=Pi0ynNDpmTJMf87i&token=0edc0814150e229f53eb628f6c66cd86")
        @RequestConfig(resultClz = WanandroidResult.class, retryCount = 3)
        Call<JSONObject> adultlist(@Body JSONObject body);

    }

    private static final NewHealthApiService newHealthApiService;

    static {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://szhc.newhealth.com.cn/")
                .addConverterFactory(Retrofit2ConverterFactory.create())
                .build();
        newHealthApiService = retrofit.create(NewHealthApiService.class);
    }

    public static void main(String[] args) {
        JSONObject adultlist = adultlist();
        System.out.println(adultlist);
    }

    private static void vaccorglist() {
        String param = "{\"areaCode\":\"4403\",\"pid\":351661,\"areaId\":\"4403\",\"numPerPage\":100,\"pageNum\":1,\"vaccCode\":\"5502\",\"reseCorpCode\":\"\",\"outpName\":\"\",\"vaccCrowd\":\"1\"}";
        int index = 1;
        while (true) {
            try {
                System.out.println(String.format("第%d次请求开始", index));
                JSONObject body = newHealthApiService.vaccorglist(JSON.parseObject(param)).execute().body();
                System.out.println(String.format("请求结果信息:%s", body.getString("message")));
                for (Object o : body.getJSONObject("data").getJSONArray("list")) {
                    JSONObject jsonObject = ((JSONObject) o);
                    if (Objects.equals("1", jsonObject.getString("flag"))) {
                        System.out.println(jsonObject);
                        play();
                        return;
                    }
                }
                System.out.println(String.format("第%d次请求没有可预约的社康", index));
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            try {
                long millis = RandomUtil.randomLong(1000, 3000);
                System.out.println(String.format("第%d次请求结束睡眠：%d", index, millis));
                Thread.sleep(millis);
                index++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static JSONObject adultlist() {
        String param = "{\"pid\":351661,\"vaccCrowd\":\"1\"}";
        try {
            JSONObject body = newHealthApiService.adultlist(JSON.parseObject(param)).execute().body();
            List<JSONObject> jsonObjects = JSON.parseArray(body.getJSONObject("data").getString("registerUser"), JSONObject.class);
            return StreamEx.of(jsonObjects)
                    .findFirst(jsonObject -> REUS_TRUE_NAME.equals(jsonObject.getString("reusTrueName")))
                    .orElse(null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void play() {
        try {
            File file = new File("D:\\development\\java\\project\\zlq\\src\\main\\resources\\static\\yiyi.mp3");
            Player player = new Player(new BufferedInputStream(new FileInputStream(file)));
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
